<template>
  <!-- 查看日常验收记录 -->
  <base-breadcrumb :title="$route.meta.title">
    <a-spin :spinning="loading">
      <!-- 基础信息 -->
      <a-card :bordered="false" class="base-info-style">
          <!-- 基本信息 -->
          <BaseInfo ref="BaseInfo" :form="form" :onlyRead="true" />
      </a-card>
      <!-- 单位工程/阶段验收/专项验收 -->
      <a-card :bordered="false">
        <title-name title="验收信息"></title-name>
        <!-- 表单信息 -->
        <custom-form
          ref="ruleForm"  
          isValidate
          :formList="formList" 
          :form="form" 
          :editModel="'V'"
        >
          <!-- 附件上传 -->
          <template #files>
            <file-link :fileList="form.fileList" style="margin-top: -3px"></file-link>
          </template>
          <template #contents="scope">
            <div v-if="scope.data.value">
              <a-card :bordered="false" v-for="item in scope.data.value" :key="item.id">
                <custom-form
                  isValidate
                  :formList="formList1" 
                  :form="item" 
                  :editModel="'V'"
                >
                </custom-form>
              </a-card>
            </div>
          </template>
        </custom-form>
      </a-card>
      <a-card :bordered="false" style="margin-bottom: 50px" v-if="(editModel == '0' && (form.status == '4' || form.status == '5')) || editModel != '0'">
        <title-name title="正式验收信息"></title-name>
        <!-- 表单信息 -->
        <custom-form
          ref="ruleFormAgain"  
          isValidate
          :formList="formListAgain" 
          :form="formAgain" 
          :editModel="onlyRead ? 'V' : 'E'"
        >
          <!-- 附件上传 -->
          <template #files>
            <template v-if="!onlyRead">
              <l-table businessCode="QM15" :uploadArrys.sync="formAgain.fileList"></l-table>
              <!-- <div class="file_prompt">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</div> -->
            </template>
            <file-link v-else :fileList="formAgain.fileList" style="margin-top: -3px"></file-link>
          </template>
        </custom-form>
      </a-card>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div
      class="btn_box page-btn-right-top"
    >
      <BasePrintButton
        v-if="editModel == 0"
        :beforePrintFn="() => getPritContent('CSCEC8XN-EQ-B30608')"
        class="button_right"
      />
      <a-button
        :loading="submitLoading"
        v-if="!onlyRead"
        type="primary"
        class="button_right"
        @click="handleSubmit()"
        >提交</a-button
      >
      <a-button type="primary" @click="$multiTab.close()">关闭</a-button>
    </div>
  </base-breadcrumb>
</template>

<script>
import { acceptanceRecord } from '@/api/quality/quality' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import BaseInfo from './BaseInfo' //
import BasePrintButton from '@/components/BasePrintButton' // 打印按钮
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    BaseInfo,
    BasePrintButton,
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      id: '',
      detailId: '',
      // 加载数据方法 必须为 Promise 对象
      formAgain: {
        acceptanceDate: '',
        statusCode: '',
        statusName: '',
        id: '',
        fileList: [],
      },
      editModel: '0',
      form: {
        projectId:"",
        filler: this.$userInfo().name,
        fillerDate: this.$dateFormat(Date.now(), 'D'),
        projectConstructionTypeName: '',
        parentTypeName: '',
        wbsName: '',
        acceptanceDatePlan: '',
        acceptorName: '',
        part: '',
      } // 表单
    }
  },
  created() {
    if (this.id) {
      this.loading = true
      acceptanceRecord
      .getDetail({
        id: this.id
      })
      .then((res) => {
        this.loading = false
        if (res.data) {
          this.initData(res.data)
        }
      })
      .catch(() => {
        this.loading = false
      })
    } else if (this.detailId) {
      this.loading = true
      acceptanceRecord
      .getDetailId({
        id: this.detailId
      })
      .then((res) => {
        this.loading = false
        if (res.data) {
          this.initData(res.data)
        }
      })
      .catch(() => {
        this.loading = false
      })
    }
  },
  computed: {
    // 只读模式
    onlyRead() {
      return this.editModel == 0
    },
    hasId() {
      return this.id != '' || this.detailId != ''
    },
    formListAgain() {
      let list = [
        {
          keyName: 'acceptanceDate',
          type: 'a-date-picker',
          label: '验收时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD',
            disabledDate: (current) => {
              return current && current < this.moment().startOf('day')
            },
          },
          rule: { required: true, message: '请选择验收时间', trigger: 'change' }
        },
        {
          label: '验收结果',
          keyName: 'statusCode',
          viewName: 'statusName',
          type: 'a-select',
          data: [
            {
              label: '验收通过',
              value: '0'
            },
            {
              label: '验收未通过',
              value: '1'
            }
          ],
          rule: { required: true, message: '请选择验收结果', trigger: 'change' },
        },
        {
          slot: true,
          keyName: 'files',
          label: '附件上传',
          span: 24
        }
      ]
      return list
    },
    formList1() {
      let list = [
          {
            keyName: 'workUtilNames',
            type: 'a-input',
            label: '作业单位',
            disabled: true
          },
          {
            keyName: 'acceptorNames',
            type: 'a-input',
            label: '验收人',
            disabled: true
          },
          {
            keyName: 'acceptanceDate',
            type: 'a-input',
            label: '验收时间',
            disabled: true
          },
          {
            keyName: 'acceptanceResultName',
            type: 'a-input',
            label: '验收结果',
            disabled: true
          },
          {
            keyName: 'rectificationContent',
            type: 'BaseTextarea',
            label: '验收记录/整改内容',
            attrs: {
                maxLength: 3000
            },
            span: 24,
            rule: { required: true, message: '请输入验收记录/整改内容', trigger: 'change' }
        },
        {
          keyName: 'imageList',
          label: '图片上传',
          businessCode: 'QM15',
          attrs: {
            uploadType: 'image',
            editModel: '0'
          },
          type: 'uploadFile',
          span: 24
        }
      ]
      return list
    },
    formList() {
      let list = [
          {
            keyName: 'checkAcceptanceTypeName',
            type: 'a-input',
            label: '质量验收类型',
            disabled: true
          },
          {
            keyName: 'unitEngineering',
            type: 'a-input',
            label: '验收内容',
            disabled: true
          },
          {
            keyName: 'wbsName',
            type: 'a-input',
            label: 'WBS',
            disabled: true
          },
          {
            keyName: 'acceptanceDatePlan',
            type: 'a-input',
            label: '计划验收时间',
            disabled: true
          },
          {
            keyName: 'acceptorName',
            type: 'a-input',
            label: '验收负责人',
            disabled: true
          },
          {
            slot: true,
            keyName: 'contents',
            type: 'a-input',
            label: '',
            span: 24
          },
      ]
      return list
    } // 表单列表
  },
  methods: {
    initData(data) {
      this.form = data
      this.formAgain.acceptanceDate = data.acceptanceDate
      this.formAgain.fileList = data.fileList
      this.formAgain.statusCode = data.statusCode
      this.formAgain.statusName = data.statusName
      this.formAgain.id = data.id
      this.form.viewName = data.branchComName + '/' + data.projectName
    },
    handleSubmit() {
      this.$refs.ruleFormAgain.validate((e) => {
        if (e) {
          this.$confirm({
              titile: '提示',
              content: '确认提交，提交后不可撤回？',
              onOk: () => {
                const formAgain = this.$clone(this.formAgain)
                if (formAgain.fileList.length == 0) {
                  this.$message.warning('请上传附件！')
                  return false
                }
                this.submitLoading = true
                acceptanceRecord['again'](formAgain)
                .then((res) => {
                  this.submitLoading = false
                  if (res.data) {
                    this.$message.success('提交成功！')
                    this.$multiTab.close()
                  }
                })
                .catch(() => {
                  this.submitLoading = false
                })
              }
            })
        }
      })
    }
  },
}
</script>
<style lang="less" scoped>
::v-deep {
  .ant-upload-picture-card-wrapper {
    width: calc(100% - 135px);
  }
}
</style>
